package com.yc.cloud.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.cloud.admin.dto.request.MemberAccountCreateParam;
import com.yc.cloud.admin.service.MemberAccountService;
import com.yc.cloud.admin.vo.MemberAccountVo;
import com.yc.cloud.basic.constants.GlobalConstants;
import com.yc.cloud.basic.constants.TipsConstants;
import com.yc.cloud.basic.dto.BaseQueryParam;
import com.yc.cloud.basic.exception.Asserts;
import com.yc.cloud.entity.MemberAccount;
import com.yc.cloud.mapper.MemberAccountMapper;
import com.yc.cloud.oss.service.OssFileService;
import com.yc.cloud.security.utils.AdminUtils;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 会员 服务实现类
 * </p>
 *
 * @author Yancy
 * @since 2023-12-30
 */
@Service
public class MemberAccountServiceImpl extends ServiceImpl<MemberAccountMapper, MemberAccount> implements MemberAccountService {

    private final OssFileService ossFileService;

    private final PasswordEncoder passwordEncoder;

    public MemberAccountServiceImpl(OssFileService ossFileService, PasswordEncoder passwordEncoder) {
        this.ossFileService = ossFileService;
        this.passwordEncoder = passwordEncoder;
    }

    @Resource
    private MemberAccountMapper memberAccountMapper;

    @Override
    public IPage<MemberAccountVo> list(BaseQueryParam queryParam) {
        val query = new QueryWrapper<MemberAccount>();
        IPage<MemberAccount> page;
        //排序
        if (StrUtil.isNotEmpty(queryParam.getSortBy())) {
            if (BaseQueryParam.ORDER_DESC.equals(queryParam.getOrder())) {
                query.orderByDesc(MemberAccount.ID);
            } else if (BaseQueryParam.ORDER_ASC.equals(queryParam.getOrder())) {
                query.orderByAsc(MemberAccount.ID);
            }
        }
        //模糊查询
        if (StrUtil.isNotEmpty(queryParam.getKeyword())) {
            query.and(queryWrapper -> {
                queryWrapper.like(MemberAccount.USERNAME, queryParam.getKeyword()).or().like(MemberAccount.TRUE_NAME, queryParam.getKeyword());
            });
        }
        //分页
        if (queryParam.getPage()) {
            page = new Page<>(queryParam.getPageNum(), queryParam.getPageSize(), true);
        } else {
            page = new Page<>(queryParam.getPageNum(), memberAccountMapper.selectCount(query), true);
        }
        val result = memberAccountMapper.selectPage(page, query);
        return result.convert(this::convertVo);
    }

    @Override
    public MemberAccount create(MemberAccountCreateParam param) {
        //判断用户名是否存在
        if (getByUsername(param.getUsername()) != null) {
            Asserts.fail(TipsConstants.USERNAME_EXIST);
        }
        val memberAccount = new MemberAccount();
        BeanUtils.copyProperties(param, memberAccount);
        memberAccount.setStatus(GlobalConstants.STATUS_ENABLE);
        memberAccount.setCreateTime(LocalDateTime.now());
        //将密码进行加密操作
        String encodePassword = passwordEncoder.encode(param.getPassword());
        memberAccount.setPassword(encodePassword);
        memberAccount.setCompanyId(AdminUtils.getCurrentCompanyId());
        val result = baseMapper.insert(memberAccount);
        if (result > 0) {
            return memberAccount;
        }
        return null;
    }

    private MemberAccount getByUsername(String username) {
        return baseMapper.selectOne(new QueryWrapper<MemberAccount>().eq(MemberAccount.USERNAME, username));
    }

    private MemberAccountVo convertVo(MemberAccount memberAccount) {
        if (memberAccount == null) {
            return null;
        }
        val vo = new MemberAccountVo();
        BeanUtils.copyProperties(memberAccount, vo);
        val iconUrl = ossFileService.getOssFileUrl(vo.getIcon());
        vo.setIconUrl(iconUrl);
        return vo;
    }
}
